package cn.edu.seig.textbook.controller;


import cn.edu.seig.textbook.domain.pojo.Requests;
import cn.edu.seig.textbook.domain.pojo.Teachers;
import cn.edu.seig.textbook.enums.RequestEnum;
import cn.edu.seig.textbook.service.IRequestsService;
import cn.edu.seig.textbook.service.ITeachersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author day
 * @since 2024-11-07
 */
@Controller

public class RequestsController {
    @Autowired
    private ITeachersService teachersService;
    @Autowired
    private IRequestsService requestsService;

    @RequestMapping("/admin/teacherRequest")
    public String teacherRequest(Model model) {
        List<Requests> requests = requestsService.findAll();
        model.addAttribute("requests", requests);
        return "admin/updateRequest";
    }

    //同意申请
    @PostMapping("/admin/approveRequest/{requestId}")
    public String approveApplication(@PathVariable int requestId) {
        Requests request = requestsService.getById(requestId);
        if (request != null) {
            request.setRequestStatus(RequestEnum.PASS);
            requestsService.updateRequest(request);
            // 更新状态
        }
        return "redirect:/admin/teacherRequest";
    }

    // 拒绝申请
    @PostMapping("/admin/rejectRequest/{requestId}")
    public String rejectApplication(@PathVariable int requestId) {
        Requests request = requestsService.getById(requestId);
        if (request != null) {
            request.setRequestStatus(RequestEnum.NOTPASS);
            requestsService.updateRequest(request);
            // 更新状态
        }
        return "redirect:/admin/teacherRequest";
    }



    @RequestMapping("/teacher/teacherRequest")
    public String showApplicationForm(Model model) {
       // 获取所有教师
       List<Teachers> teachers = teachersService.findAll();

       model.addAttribute("teachers", teachers);
       // 新的申请对象
       model.addAttribute("request", new Requests());
       return "teacher/teacherRequests";
    }

    @PostMapping("/teacher/Request_add")
    public String submitApplication(Requests request) {
       request.setRequestStatus(RequestEnum.NOTPASS);
       requestsService.addRequest(request);
        // 保存申请
        return "redirect:/teacher/success";
        // 提交成功后跳转
    }

    //跳转到成功页面
    @GetMapping("teacher/success")
    public String successPage() {
        return "teacher/success";
    }


}
